package com.insight.common.exception;

/**
 * T022: 数据库连接异常
 * 当数据源连接测试失败或连接过程中发生错误时抛出
 */
public class DatabaseConnectionException extends RuntimeException {

    /**
     * 数据库类型
     */
    private final String databaseType;

    /**
     * 数据库主机地址
     */
    private final String host;

    /**
     * 数据库端口
     */
    private final Integer port;

    public DatabaseConnectionException(String databaseType, String host, Integer port, String message) {
        super(String.format("Failed to connect to %s database at %s:%d - %s", databaseType, host, port, message));
        this.databaseType = databaseType;
        this.host = host;
        this.port = port;
    }

    public DatabaseConnectionException(String databaseType, String host, Integer port, Throwable cause) {
        super(String.format("Failed to connect to %s database at %s:%d", databaseType, host, port), cause);
        this.databaseType = databaseType;
        this.host = host;
        this.port = port;
    }

    public DatabaseConnectionException(String message) {
        super(message);
        this.databaseType = null;
        this.host = null;
        this.port = null;
    }

    public DatabaseConnectionException(String message, Throwable cause) {
        super(message, cause);
        this.databaseType = null;
        this.host = null;
        this.port = null;
    }

    public String getDatabaseType() {
        return databaseType;
    }

    public String getHost() {
        return host;
    }

    public Integer getPort() {
        return port;
    }
}
